package com.googlecode.uipipedream;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.googlecode.uipipedream.impl.ColourPanel;
import com.googlecode.uipipedream.impl.ColourSwatch;
import com.googlecode.uipipedream.impl.HorizontalSliderBar;
//import com.googlecode.uipipedream.impl.HorizontalSliderBar;
import com.googlecode.uipipedream.impl.VerticalSliderBar;
import com.googlecode.uipipedream.spec.ColourModel;
import com.googlecode.uipipedream.spec.HSBType;

//public class Main extends Applet implements Runnable {
public class Main extends JFrame implements Runnable {
	private static final long serialVersionUID = 2880337302963035000L;
	private ColourModel model = new ColourModel();
	private ColourPanel colourPanel;
	
	public static void main(String[] args) {
		java.awt.EventQueue.invokeLater(new Main());
	}
	
	@SuppressWarnings("deprecation")
	public boolean mouseDown(Event e, int x, int y) {
		if (colourPanel.getBounds().contains(x, y)) {
			oldSwatch = null;
			Container f = this;
			while (f != null && !(f instanceof Frame))
				f = f.getParent();
			if (f != null)
				((Frame) f).setCursor(Frame.CROSSHAIR_CURSOR);
			return true;
		} else

		return false;
	}

	@SuppressWarnings("deprecation")
	public boolean mouseUp(Event e, int x, int y) {
		return false;
//		if (swatchDrag) {
//			Container f = this;
//			while (f != null && !(f instanceof Frame))
//				f = f.getParent();
//			if (f != null)
//				((Frame) f).setCursor(Frame.DEFAULT_CURSOR);
//			return true;
//		}
//		return false;
	}

	private ColourSwatch oldSwatch = null;
	private Color oldColor = null;

	public boolean mouseDrag(Event e, int x, int y) {
//		if (swatchDrag == false)
//			return false;
//
//		ColourSwatch swatch = swatchLocate(x, y);
//		if (swatch != oldSwatch) {
//			if (oldSwatch != null)
//				oldSwatch.setColor(oldColor);
//			if (swatch != null) {
//				oldColor = swatch.getColor();
//				swatch.setColor(model.getColor());
//			}
//			oldSwatch = swatch;
//		}
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		// 1. Setup how this window looks and acts.
		this.setTitle("Assignment Four: A Novel Colour Selector");
		this.setMinimumSize(new Dimension(600,300));
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		// 2. Setup components.
		this.getContentPane().setBackground(Color.black);
		this.colourPanel = new ColourPanel(model);
		
		// 3. Add components to the window.
		JPanel sliderPanel = new JPanel();
		sliderPanel.setLayout(new FlowLayout());
		sliderPanel.setBackground(Color.black);
		sliderPanel.add(new VerticalSliderBar(model, HSBType.SATURATION));
		sliderPanel.add(new VerticalSliderBar(model, HSBType.BRIGHTNESS));
		this.setLayout(new BorderLayout());
		this.add("West", new HorizontalSliderBar(model, HSBType.HUE));
		this.add("East", sliderPanel);
//		this.add("South", this.swatchPanel);
		
		// 4. Set inital colours
		this.model.setBrightness(1f);
		this.model.setSaturation(1f);
		this.model.setColor(Color.red);
		this.validate();
		
		// 5. Display the window and start this application.
		this.setLocation(100,100);
		this.pack();
		this.setVisible(true);
	}
}
